﻿// -----------------------------------------------------------------------
// <copyright file="HoSoKhamBenhNgoaiTruDataAccess.cs" company="">
// TODO: Copyright by dangppsoft@gmail.com.
// </copyright>
// -----------------------------------------------------------------------
namespace ManagerClinic.Core.DataAccess
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ManagerClinic.Core.Common;
    using System.Data.Common;
    using System.Data;
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using Microsoft.Practices.EnterpriseLibrary.Common;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    using ManagerClinic.Ultilities.Ultility;

    public class HoSoKhamBenhNgoaiTruDataAccess
    {
        public bool InsertHoSoKhamBenhNgoaiTru(HoSoKhamBenhNgoaiTruEntity obj)
        {
            try
            {
                string sql = "spHoSoKhamBenhNgoaiTru_Insert";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaHoSo", DbType.String, obj.MaHoSo);
                db.AddInParameter(cmd, "SoVaoVien", DbType.String, obj.SoVaoVien);
                db.AddInParameter(cmd, "Stt", DbType.Int32, obj.Stt);
                db.AddInParameter(cmd, "MaBenhNhan", DbType.String, obj.MaBenhNhan);
                db.AddInParameter(cmd, "NgayVaoKham", DbType.DateTime, obj.NgayVaoKham);
                db.AddInParameter(cmd, "TrangThaiPhieu", DbType.Int32, obj.TrangThaiPhieu);
                db.AddInParameter(cmd, "GhiChu", DbType.String, obj.GhiChu);
                db.AddInParameter(cmd, "LyDoVaoKham", DbType.String, obj.LyDoVaoKham);
                db.AddInParameter(cmd, "ChanDoan", DbType.String, obj.ChanDoan);
                db.AddInParameter(cmd, "KetLuan", DbType.String, obj.KetLuan);
                db.AddInParameter(cmd, "MaXuTri", DbType.String, obj.MaXuTri);
                db.AddInParameter(cmd, "MaBacSy", DbType.String, obj.MaBacSy);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("InsertHoSoKhamBenhNgoaiTru", ex.Message); throw ex; }
        }

        public bool EditHoSoKhamBenhNgoaiTru(HoSoKhamBenhNgoaiTruEntity obj)
        {
            try
            {
                string sql = "spHoSoKhamBenhNgoaiTru_Update";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaHoSo", DbType.String, obj.MaHoSo);
                //db.AddInParameter(cmd, "SoVaoVien", DbType.String, obj.SoVaoVien);
                //db.AddInParameter(cmd, "Stt", DbType.Int32, obj.Stt);
                db.AddInParameter(cmd, "MaBenhNhan", DbType.String, obj.MaBenhNhan);
                //db.AddInParameter(cmd, "NgayVaoKham", DbType.DateTime, obj.NgayVaoKham);
                db.AddInParameter(cmd, "TrangThaiPhieu", DbType.Int32, obj.TrangThaiPhieu);
                db.AddInParameter(cmd, "GhiChu", DbType.String, obj.GhiChu);
                db.AddInParameter(cmd, "LyDoVaoKham", DbType.String, obj.LyDoVaoKham);
                db.AddInParameter(cmd, "ChanDoan", DbType.String, obj.ChanDoan);
                db.AddInParameter(cmd, "KetLuan", DbType.String, obj.KetLuan);
                db.AddInParameter(cmd, "MaXuTri", DbType.String, obj.MaXuTri);
                db.AddInParameter(cmd, "MaBacSy", DbType.String, obj.MaBacSy);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("UpdateHoSoKhamBenhNgoaiTru", ex.Message); throw ex; }
        }

        public bool DeleteHoSoKhamBenhNgoaiTru(string maHoSo)
        {
            try
            {
                string sql = "spHoSoKhamBenhNgoaiTru_DeleteByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaHoSo", DbType.String, maHoSo);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("DeleteHoSoKhamBenhNgoaiTru", ex.Message); throw ex; }
        }

        public HoSoKhamBenhNgoaiTruEntity GetHoSoKhamBenhNgoaiTru(string maHoSo)
        {
            try
            {
                HoSoKhamBenhNgoaiTruEntity obj = null;
                string sql = "spHoSoKhamBenhNgoaiTru_SelectByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaHoSo", DbType.String, maHoSo);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new HoSoKhamBenhNgoaiTruEntity();
                        obj.MaHoSo = reader["MaHoSo"].ToString();
                        obj.SoVaoVien = reader["SoVaoVien"].ToString();
                        obj.Stt = String.IsNullOrEmpty(reader["Stt"].ToString()) == true ? 0 : (Int32)reader["Stt"];
                        obj.MaBenhNhan = reader["MaBenhNhan"].ToString();
                        obj.NgayVaoKham = String.IsNullOrEmpty(reader["NgayVaoKham"].ToString()) == true ? DateTime.MinValue : (DateTime)reader["NgayVaoKham"];
                        obj.TrangThaiPhieu = String.IsNullOrEmpty(reader["TrangThaiPhieu"].ToString()) == true ? 0 : (Int32)reader["TrangThaiPhieu"];
                        obj.GhiChu = reader["GhiChu"].ToString();
                        obj.LyDoVaoKham = reader["LyDoVaoKham"].ToString();
                        obj.ChanDoan = reader["ChanDoan"].ToString();
                        obj.KetLuan = reader["KetLuan"].ToString();
                        obj.MaXuTri = reader["MaXuTri"].ToString();
                        obj.MaBacSy = reader["MaBacSy"].ToString();
                    }
                }
                return obj;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetHoSoKhamBenhNgoaiTru", ex.Message); throw ex; }
        }

        public List<HoSoKhamBenhNgoaiTruEntity> GetHoSoKhamBenhNgoaiTruList(DateTime ngayVaoKham)
        {
            try
            {
                List<HoSoKhamBenhNgoaiTruEntity> list = new List<HoSoKhamBenhNgoaiTruEntity>();
                HoSoKhamBenhNgoaiTruEntity obj = null;
                string sql = "spHoSoKhamBenhNgoaiTru_SelectAll";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "NgayVaoKham", DbType.DateTime, ngayVaoKham);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new HoSoKhamBenhNgoaiTruEntity();
                        obj.MaHoSo = reader["MaHoSo"].ToString();
                        obj.SoVaoVien = reader["SoVaoVien"].ToString();
                        obj.Stt = String.IsNullOrEmpty(reader["Stt"].ToString()) == true ? 0 : (Int32)reader["Stt"];
                        obj.MaBenhNhan = reader["MaBenhNhan"].ToString();
                        obj.NgayVaoKham = String.IsNullOrEmpty(reader["NgayVaoKham"].ToString()) == true ? DateTime.MinValue : (DateTime)reader["NgayVaoKham"];
                        obj.TrangThaiPhieu = String.IsNullOrEmpty(reader["TrangThaiPhieu"].ToString()) == true ? 0 : (Int32)reader["TrangThaiPhieu"];
                        obj.GhiChu = reader["GhiChu"].ToString();
                        obj.LyDoVaoKham = reader["LyDoVaoKham"].ToString();
                        obj.ChanDoan = reader["ChanDoan"].ToString();
                        obj.KetLuan = reader["KetLuan"].ToString();
                        obj.MaXuTri = reader["MaXuTri"].ToString();
                        obj.MaBacSy = reader["MaBacSy"].ToString();

                        obj.TenBenhNhan = reader["TenBenhNhan"].ToString();
                        obj.NgaySinh = reader["NgaySinh"].ToString();
                        obj.ThangSinh = reader["ThangSinh"].ToString();
                        obj.NamSinh = String.IsNullOrEmpty(reader["NamSinh"].ToString()) == true ? 0 : (Int32)reader["NamSinh"];
                        obj.ThonPho = reader["ThonPho"].ToString();
                        obj.NoiLamViec = reader["NoiLamViec"].ToString();
                        obj.SoDienThoai = reader["SoDienThoai"].ToString();
                        obj.Email = reader["Email"].ToString();
                        obj.TenGioiTinh = reader["TenGioiTinh"].ToString();
                        obj.TenQuocGia = reader["TenQuocGia"].ToString();
                        obj.TenTinhThanh = reader["TenTinhThanh"].ToString();
                        obj.TenQuanHuyen = reader["TenQuanHuyen"].ToString();
                        list.Add(obj);
                    }
                }
                return list;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetHoSoKhamBenhNgoaiTruList", ex.Message); throw ex; }
        }

    }
}